/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.PhieuBanHangDTO;

/**
 *
 * @author tuan
 */
public class PhieuBanHangDAO {
    private static final String MaPhieuBH = "MaPBH000";
    private static final int lengthMa = 8;
    
    public PhieuBanHangDAO() {
    }
    
    public String getNewMaPhieuBanHang() throws SQLException {
        String MaPBH = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllPhieuBanHangFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAPHIEUBH").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaPBH = MaPBH.concat(MaPhieuBH + maxMa);
        return MaPBH;
    }
    
    public ArrayList<PhieuBanHangDTO> SearchPhieuBanHangByRule(PhieuBanHangDTO phieuBH) throws SQLException {
        ArrayList<PhieuBanHangDTO> listphieuBH = new ArrayList<>();
        Object[] objs = { phieuBH.getMaPhieuBanHang(), phieuBH.getMaKhachHang(), phieuBH.getNgayBan(), 
                    phieuBH.getTongTien()};
        ResultSet result 
                = SQLServerConnect.getInstance().getDataFromStore(
                        "GetPhieuBanHangFromDatabase(?, ?, ?, ?)", objs);
        PhieuBanHangDTO phieubanhang;
        while (result.next()) {
            phieubanhang = new PhieuBanHangDTO(result.getString("MAPHIEUBH"), result.getString("MAKHACHANG"), 
                    result.getDate("NGAYBAN"), result.getLong("TONGTIEN"));
            listphieuBH.add(phieubanhang);
        }
        return listphieuBH;
    }
    
    public PhieuBanHangDTO GetPhieuBanHangByMaPhieuBH(String MaPhieuBH) throws SQLException {
        Object[] objs = { MaPhieuBH };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetPhieuBanHangByMaPhieuBHDatabase( ? )", objs);
        PhieuBanHangDTO phieubanhang = null;
        while (result.next()) {
            phieubanhang = new PhieuBanHangDTO(result.getString("MAPHIEUBH"), result.getString("MAKHACHHANG"),  result.getDate("NGAYBAN"),
                        result.getLong("TONGTIEN"));
        }
        return phieubanhang;
    }
    
    public int UpdatePhieuBanHang(PhieuBanHangDTO phieuBH) {
        Object[] objs = { phieuBH.getMaPhieuBanHang(), phieuBH.getMaKhachHang(), phieuBH.getNgayBan(), 
                    phieuBH.getTongTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdatePhieuBANHangToDatabase(? ,?, ?, ?)", objs);
    }

    public int DeletePhieuBanHang(PhieuBanHangDTO phieuBH) {
        Object[] objs = { phieuBH.getMaPhieuBanHang()};
        return SQLServerConnect.getInstance().updateDataToDatabase("DeletePhieuBanHangToDatabase(? )", objs);
    }
    
    public int InsertPhieuBanHang(PhieuBanHangDTO phieuBH) {
        Object[] objs = { phieuBH.getMaPhieuBanHang(), phieuBH.getMaKhachHang(), phieuBH.getNgayBan(), 
                    phieuBH.getTongTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertPhieuBanHangToDatabase(? ,?, ?, ?)", objs);
    }
}
